﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Ext.Net;
using ENT.GP.Core.Autenticacion;
using ENT.GP.Core.Core;
using ENT.GP.Core.Mail;
using System.Configuration;
using ENT.GP.Core;
using entgp.Code;
//wucViewProyecto1_ctl01
public partial class _Default : PaginasUsuario 
{
    private bool resp;
    protected void Page_Load(object sender, EventArgs e)
    {
        
    }

    protected void Button1_Click(object sender, DirectEventArgs e)
    {
        try
        {
            resp             = false;
            e.ExtraParamsResponse.Add(new Ext.Net.Parameter("message", Autenticar()));
            e.ExtraParamsResponse.Add(new Ext.Net.Parameter("location", resp.ToString()));
           
            e.Success = true;
        }
        catch (Exception ex)
        {
            Util.RegistarLog(ex.ToString());
        }
    }

    protected void Text2_KeyPress(object sender, DirectEventArgs e)
    {
        try
        {
            resp              = false;
            e.ExtraParamsResponse.Add(new Ext.Net.Parameter("message", Autenticar()));
            e.ExtraParamsResponse.Add(new Ext.Net.Parameter("location", resp.ToString()));

            e.Success = true;
          
        }
        catch (Exception ex)
        {
            Util.RegistarLog(ex.ToString());
        }
    }

    private string Autenticar()
    {
        string sEstado        = "";
        string sReturnMessage = "";
        Autenticacion auth;
        auth=Autenticacion.Execute(txtUsername.Text, txtPassword.Text);

        if (auth.Result)
        {
            sEstado = auth.DatosUsuario.Tables[0].Rows[0][6].ToString();
            if (sEstado == "ACTIVO")
            {
                sEstado = auth.DatosUsuario.Tables[0].Rows[0][15].ToString();
                if (sEstado == "1")
                {
                    // Do some Authentication...
                    Session["IdUsuario"] = auth.DatosUsuario.Tables[0].Rows[0][0].ToString();
                    Session["Usuario"] = txtUsername.Text;
                    Session["NombreUsuario"] = string.Format("{0} {1}", auth.DatosUsuario.Tables[0].Rows[0][2].ToString(), auth.DatosUsuario.Tables[0].Rows[0][3].ToString());
                    Session["Email"] = auth.DatosUsuario.Tables[0].Rows[0][8].ToString();
                    Session["Rol"] = auth.DatosUsuario.Tables[0].Rows[0][10].ToString();
                    Session["CRol"] = auth.DatosUsuario.Tables[0].Rows[0][12].ToString();
                    Session["IdRol"] = auth.DatosUsuario.Tables[0].Rows[0][9].ToString();
                    Session["CodigoRol"] = auth.DatosUsuario.Tables[0].Rows[0][12].ToString();
                    Session["CodigoEmp"] = auth.DatosUsuario.Tables[0].Rows[0][13].ToString();
                    Session["AbrevEmp"] = auth.DatosUsuario.Tables[0].Rows[0][14].ToString();
                    //Almacenar Nombre de la cache del Rol para crear frontal
                    Session["CacheRolName"] = "";
                    //UtilP.RUTA_ARCHIVOS_EMPRESA = string.Format("{0}/resources/empresas/{1}/", ConfigurationManager.AppSettings.Get("SD"), Session["AbrevEmp"]);
                    // Then user send to application
                    resp = true;
                    //Response.Redirect("Principal.aspx");
                }
                else
                {
                    sReturnMessage = ManejadorRecursos.GetRecurso("MsgLoginEmpInact"); 
                    

                }
            }
            else
            {
               sReturnMessage = ManejadorRecursos.GetRecurso("MsgLoginInactivo") ;
              
            }
        }
        else
        {
            sReturnMessage = ManejadorRecursos.GetRecurso("MsgLoginError");
                                     
        }
        return (sReturnMessage);
        
    }
}
